<?php
/**
 * Created by PhpStorm.
 * User: chenxiongde
 * Date: 17/4/30
 * Time: 下午3:19
 */
function  p($var){
    if(is_bool($var) || is_null($var)){
      var_dump($var);
    }else{
        echo '<pre style="padding:10px;margin:10px;border:2px solid #ccc;font-size:18px;font-weight:700;">';
        print_r($var);
        echo '</pre>';
    }

}

function   config($var=null,$val=null){
    static  $config = array();
    if(is_array($var)){
        $config = array_merge($config,array_change_key_case($var,CASE_UPPER));
        return ;
    }
    if(is_string($var)){
        if(!is_null($val)){
            $config[$var] = $val;
        }
        return isset($config[$var])?$config[$var]:null;
    }
    if(is_null($var)&&is_null($val)){
        return $config;
    }
}

function  go($url,$time=0,$msg=''){
    if(!headers_sent()){
        ($time == 0) ?header('Location:'.$url):header("refresh:{$time},url={$url}");
        die($msg);
    }else{
        echo "<meta http-equiv='refresh' content='{$time}' url='{$url}'>";
        if($time)  die($msg);
    }
}


function  halt($error,$level=3,$type='',$dist=''){
    if(is_array($error)){
        Log::write($error['message'],$level,$type,$dist);
    }else{
        Log::write($error,$level,$type,$dist);
    }
    $e=array();
    if(DEBUG){
        if(!is_array($error)){
            $trace = debug_backtrace();
            p($trace);
        }
    }
}
/**
 * Created by PhpStorm.
 * User: chenxiongde
 * Date: 17/4/30
 * Time: 下午6:13
 */
class Controller{
    public function __construct()
    {
        if(method_exists($this,'__init')){
            $this->__init();
        }

    }

    protected  function  success($message='操作成功',$url=null,$time=3){
        $url = $url ?$url:'';
        include MANGO_TPL_PATH.'/dispach.html';
    }
    protected  function  error($message='操作失败',$url=null,$time=3){
        $url = $url ?$url:'';
        include MANGO_TPL_PATH.'/dispach.html';
    }
}
/**
 * Created by PhpStorm.
 * User: chenxiongde
 * Date: 17/5/1
 * Time: 上午10:28
 */
class Log{
    static public   function  write($msg,$level='ERROR',$type=3,$dist=null){
        if(!config('SAVE_LOG')) return ;
        if(is_null($dist)){
            $dist = LOG_PATH.'/'.date('Ymd').'.log';

        }
        is_dir(LOG_PATH) && error_log("[TIME]:".date('Y-m-d H:i:s')."---{$level}---"."{$msg}\r\n",$type,$dist);

    }
}
/**
 * Created by PhpStorm.
 * User: chenxiongde
 * Date: 17/4/30
 * Time: 下午3:20
 */
final  class Application{
    public  static function  run(){
        self::_init();
        self::_set_url();
        spl_autoload_register(array(__CLASS__,'_autoload'));
        self::_create_demo();
        self::_runit();
    }
    //初始化框架
    private static  function  _init(){
        config(include MANGO_CONF_PATH.'/config.php');
        $userConf =APP_CONF_PATH.'/config.php';
        $str = <<<STR
<?php
    //配置项=>配置值
    return array(

    );
?>
STR;
        is_file($userConf) || file_put_contents($userConf,$str);
        config(include $userConf);
        // 设置session开关
        config('SESSION_AUTO_START') && session_start();
        //设置时区
        date_default_timezone_set(config('DEFAULT_TIMEZONE'));

    }
    //设置外部路径

    private static function  _set_url(){
        $path = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER['HTTP_HOST'].'/'.$_SERVER['SCRIPT_NAME'];
        define('__APP__',$path);
        define('__ROOT__',dirname($path));
        define('__PUBLIC__',__ROOT__.'/'.APP_NAME.'/Public');

    }

    /**
     * 自动加载
     */
    private static function  _autoload($className){
        include APP_CONTROLLER_PATH.'/'.$className.'.php';
    }

    /**
     * 创建demo
     */
    private  static  function  _create_demo(){
        $path = APP_CONTROLLER_PATH.'/IndexController.php';
        $str = <<<STR
<?php
    class IndexController  extends Controller{
        public  function   index(){
            echo   '<h1  style="width:100%;margin:240px 35%;">欢迎使用MANGO框架</h1>';
        }
    }
?>
STR;
        is_file($path) || file_put_contents($path,$str);

    }

    /**
     * 运行框架
     */
    private static function  _runit(){
        $c = isset($_GET[config('VAR_CONTROLLER')])?config('VAR_CONTROLLER'):'Index';
        $c.='Controller';
        $a = isset($_GET[config('VAR_ACTION')])?config('VAR_ACTION'):'index';
        $obj = new $c();
        $obj->$a();
    }
}
